<template>
  <div class="admin">
    <div class="login-area" v-show="!adminLogined">
      <h5 class="text-center">管理员登陆</h5>
      <form action="" class="form-horizontal col-md-4 col-md-offset-4">
        <div class="form-group">
          <div class="col-md-10 col-md-offset-1">
            <input type="text" placeholder="admin" class="form-control" v-model="admin.name">
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-10 col-md-offset-1">
            <input type="password" placeholder="password" class="form-control" v-model="admin.password">
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-10 col-md-offset-1 text-center">
            <button @click.prevent="adminLogin" class="btn btn-sm btn-default btn-block">login</button>
          </div>
        </div>
      </form>
    </div>
    <div class="admin-area" v-if="adminLogined">
      <div class="nothing" v-show="!applys.length">无</div>
      <ul class="list-group" v-if="applys.length">
        <li class="list-group-item clearfix">
          <div class="col-md-4">文章名称</div>
          <div class="col-md-4">作者</div>
          <div class="col-md-4">操作</div>
        </li>
        <li class="list-group-item clearfix" v-for="item in applys">
          <div class="col-md-4"><router-link :to="'/article/' + item.articleId"> {{item.articleTitle}} </router-link> </div>
          <div class="col-md-4">{{item.articleAutor}} </div>
          <div class="col-md-4">
            <button class="btn btn-default" @click="noPass(item._id)">不通过</button> 
            <button class="btn btn-primary" @click="pass(item._id)">通过</button>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        adminLogined: sessionStorage.admin,
        admin: {
          name: '',
          password: ''
        },
        applys: []
      }
    },
    computed: {
      logined () {
        return this.$store.state.isLogined
      }
    },
    mounted () {
      this.$http.get(this.rootPath + '/applyBetterCheck').then((res) => {
        this.applys = res.data
      })
    },
    methods: {
      adminLogin () {
        if (this.logined) {
          return window.alert('请注销其他账号')
        }
        this.$http.post(this.rootPath + '/admin', this.admin).then((res) => {
          if (res.data === '1') {
            sessionStorage.admin = true
            this.adminLogined = true
          } else {
            window.alert('密码不正确')
          }
        })
      },
      pass (applyId) {
        this.$http.post(this.rootPath + '/applyBetterCheck', {applyId: applyId, isPass: 1}).then((res) => {
          if (res.data === '1') {
            window.alert('pass')
          } else {
            alert('已经操作过了！')
          }
        })
      },
      noPass (applyId) {
        this.$http.post(this.rootPath + '/applyBetterCheck', {applyId: applyId, isPass: 0}).then((res) => {
          if (res.data === '1') {
            window.alert('pass')
          } else {
            alert('已经操作过了！')
          }
        })
      }
    },
    props: ['rootPath']
  }
</script>